package com.vastcom.cms.action;

import com.vastcom.cms.model.Carousel;
import com.vastcom.cms.model.News;
import com.vastcom.cms.model.Singlepage;
import com.vastcom.cms.model.Users;
import com.vastcom.cms.service.ManageService;
import com.vastcom.cms.to.PageParm;
import com.vastcom.cms.to.PasswordLoginTo;
import com.vastcom.cms.tools.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/public")
public class PublicAction {
    @Autowired
    private ManageService manageService;

//    @PostMapping("getSinglepage")
//    @ResponseBody
//    public HashMap getSinglepage(@RequestBody Singlepage singlepage)throws Exception{
//        HashMap m=new HashMap();
//        Singlepage temp=manageService.getSinglepage(singlepage.getSinglepageid());
//        if(temp!=null){
//            m.put("msg","ok");
//            m.put("obj",temp);
//        }
//        else {
//            m.put("msg","empty");
//        }
//        return m;
//    }

    @GetMapping("getSinglePageList")
    @ResponseBody
    public HashMap getSinglePageList() throws Exception{
        HashMap m=new HashMap();
        try {
            List<Singlepage> sl=manageService.getSinglepageList();
            if(sl.size()>0){
                m.put("msg","ok");
                m.put("list",sl);
            }
            else {
                m.put("msg","empty");
            }
        } catch (Exception e) {
            e.printStackTrace();
            m.put("msg","error");
        }
        return m;
    }

    @GetMapping("getToken")
    @ResponseBody
    public HashMap getToken()throws Exception{
        HashMap m=new HashMap();

        String token= JwtUtil.generateToken("admin","admin456");
        m.put("token",token);
        return m;
    }



    @PostMapping("saveNews")
    @ResponseBody
    public HashMap saveNews(@RequestBody News news)throws Exception{
        HashMap m=new HashMap();

        try {
            for(int i=0;i<100;i++){
              News n=new News();
              n.setTitle(news.getTitle()+"第"+i+"条");
              n.setPbdate(new Timestamp(System.currentTimeMillis()).getTime());
              n.setContent(news.getContent()+"第"+i+"条");
              manageService.saveNews(n);

            }

            m.put("msg","ok");
        } catch (Exception e) {
            e.printStackTrace();
            m.put("msg","error");
        }

        return m;
    }
    @GetMapping("getNewsList")
    @ResponseBody
    public HashMap getNewsList()throws Exception{
        HashMap m=new HashMap();
        List<News> newslist=manageService.getNewsList();
          m.put("list", newslist);

        return m;
    }





    @PostMapping("deleteNews")
    @ResponseBody
    public HashMap deleteNews(@RequestBody News news)throws Exception{
        HashMap m=new HashMap();
        try {
            manageService.deleteNews(news.getNewsid());
            m.put("msg","ok");
        } catch (Exception e) {
            e.printStackTrace();
            m.put("msg","error");
        }


        return m;
    }

    @PostMapping("getNews")
    @ResponseBody
    public HashMap getNews(@RequestBody News news)throws Exception{
        HashMap m=new HashMap();
        News temp=manageService.geiNewsById(news.getNewsid());
        if (temp!=null){
            m.put("msg","ok");
            m.put("obj",temp);
        }
        else {
            m.put("msg","empty");
        }

        return m;
    }
    @PostMapping("getNewsListByPage")
    @ResponseBody
    public HashMap getNewsListByPage(@RequestBody PageParm pageParm)throws Exception{
        HashMap m=new HashMap();
        Page<News> nl=manageService.getNewsForPage(pageParm.getPage(),pageParm.getPagesize());
        if (nl!=null){
            m.put("msg","ok");
            m.put("obj",nl);
        }
        else {
            m.put("msg","error");

        }
        return m;
    }

    @PostMapping("getNewsListByCate")
    @ResponseBody
    public HashMap getNewsListByCate(@RequestBody PageParm pageParm)throws Exception{
        HashMap m=new HashMap();
        Page<News> nl=manageService.getNewsByPage(pageParm.getNewscate(),pageParm.getPage(),pageParm.getPagesize());
        if(nl!=null){
            m.put("msg","ok");
            m.put("obj",nl);
        }
        else {
            m.put("msg","error");
        }

        return m;
    }


    @GetMapping("getCarouselList")
    @ResponseBody
    public HashMap getCarouselList()throws Exception{
        HashMap m=new HashMap();
        List<Carousel> cl=manageService.getCarouselList();
        if(cl.size()>0){
            m.put("msg","ok");
            m.put("list",cl);
        }
        else {
            m.put("msg","empty");
        }

        return m;
    }

    @PostMapping("/login")
    @ResponseBody
    public HashMap login(@RequestBody PasswordLoginTo passwordLoginTo)throws Exception{
        HashMap m=new HashMap();
//        if(passwordLoginTo.getUsername().equals("admin")&&passwordLoginTo.getPassword().equals("123456")){
//
//            m.put("msg","ok");
//            String token= JwtUtil.generateToken("admin","testuserid");
//            m.put("token",token);
//        }
//        else {
//            m.put("msg","passerror");
//        }
        Users u=manageService.getUserByUserNameAndPass(passwordLoginTo.getUsername(),passwordLoginTo.getPassword());
        if (u!=null){
            m.put("msg","ok");
            String token= JwtUtil.generateToken(u.getRole(),u.getUserid());
            m.put("token",token);
        }
        else {
            m.put("msg","passerror");
        }




        return m;
    }


    @PostMapping("regist")
    @ResponseBody
    public HashMap regist(@RequestBody Users users)throws Exception{
        HashMap m=new HashMap();

        try {
            users.setRole("member");
            if(manageService.isExsistUserName(users.getUsername())){
                m.put("msg","exsistusername");
            }
            else {
                manageService.saveUser(users);
                m.put("msg","ok");
            }


        } catch (Exception e) {
            e.printStackTrace();
            m.put("msg","error");
        }
        return m;
    }






}
